package com.csyf.manager.service.impl;

import com.csyf.manager.channel.base.ServiceEnum;
import com.csyf.manager.dao.SysAgentChannelDao;
import com.csyf.manager.dao.SysAliAccountDao;
import com.csyf.manager.dao.SysMerchDao;
import com.csyf.manager.entity.SysAgentChannel;
import com.csyf.manager.entity.SysChannelAttr;
import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.entity.SysTradeType;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.service.SysChannelBiz;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @Author: Enzo
 * @Date: 2018/4/1 15:33
 */
@Slf4j
@Service
public class SysChannelBizImpl implements SysChannelBiz {

    @Autowired
    private SysMerchDao sysMerchDao;

    @Autowired
    private SysAgentChannelDao sysAgentChannelDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private SysAliAccountDao sysAliAccountDao;

    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;


    private final static boolean PAY_LIMIT = true;

    @Override
    public SysAgentChannel queryByMchid(String mch_id, String src_mch_id) {
        SysAgentChannel record = new SysAgentChannel();
        record.setMchId(mch_id);
        record.setMerchantNo(src_mch_id);
        return sysAgentChannelDao.selectOne(record);
    }

    @Override
    public List<SysAgentChannel> queryByMchId(String mchId) {
        SysAgentChannel record = new SysAgentChannel();
        record.setMchId(mchId);
        return sysAgentChannelDao.select(record);
    }

    @Override
    public SysAgentChannel handleMchant(String mch_id, String service) throws ApiValidateException {


        SysAgentChannel record = new SysAgentChannel();
        List<SysAgentChannel> select = new ArrayList<>();

        SysChannelAttr tradeByService = sysChannelAttrBiz.findTradeByService(service);
        if (null == tradeByService) {
            throw new ApiValidateException("尚未配置上游通道号，请联系技术");
        } else {
            record.setMchId(mch_id);
            record.setChannelNo(tradeByService.getChannelName());
        }

        record.setStatus(1);
        select = sysAgentChannelDao.select(record);
        if (select.isEmpty()) {
            log.error("商户号[{}] 业务[{}],未开通", mch_id, service);
            throw new ApiValidateException("通道信息未配置");
        }

        int i = new Random().nextInt(select.size());
        SysAgentChannel sysAgentChannel = select.get(i);

        redisService.setSrcChannelInfo(sysAgentChannel.getMchId(), sysAgentChannel.getMerchantNo(), new Gson().toJson(sysAgentChannel));
        return sysAgentChannel;

    }


    /**
     * 查询代理商开通的通道
     *
     * @param agentId
     * @return
     */
    @Override
    public List<SysAgentChannel> queryByAgentId(String agentId) {
        SysAgentChannel sysAgentChannel = new SysAgentChannel();
        sysAgentChannel.setAgentId(Long.valueOf(agentId));
        return sysAgentChannelDao.select(sysAgentChannel);
    }

    @Override
    public Map<String, String> queryAgentsByService(String channelNo) {
        Map<String, String> agentMap = new HashMap<>();

//        SysAgentChannel vo = new SysAgentChannel();
//        vo.setChannelNo(channelNo);
//        vo.setStatus(1);
//        List<SysAgentChannel> sysAgentChannelList = sysAgentChannelDao.select(vo);
//
//        for (SysAgentChannel agentChannel : sysAgentChannelList) {
//            agentMap.put(agentChannel.getAgentId() + "", agentChannel.getMchId());
//        }

        List<SysMerchant> sysAgents = sysMerchDao.selectAll();
        for (SysMerchant sysAgent : sysAgents) {
            agentMap.put(sysAgent.getId() + "", sysAgent.getMerchMerchantNo());
        }


        return agentMap;
    }

    /**
     * 查询支付通道列表
     *
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @Override
    public JqGridVo querydata(SysAgentChannel vo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("mch_id desc");

        Example example = new Example(SysAgentChannel.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "agentId", vo.getAgentId());
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "merchantNo", vo.getMerchantNo());
        PageUtils.andLikeTo(criteria, "channelNo", vo.getChannelNo());
        List<SysAgentChannel> select = sysAgentChannelDao.selectByExample(example);

        JqGridVo jqGridVo = PageUtils.pasrJqGrid(select);
        return jqGridVo;
    }

    /**
     * 更新支付通道状态
     *
     * @param vo
     */
    @Override
    public void updateChannelStatus(SysAgentChannel vo) {

        SysAgentChannel entity = new SysAgentChannel();
        entity.setId(vo.getId());
        entity.setStatus(vo.getStatus());
        sysAgentChannelDao.updateByPrimaryKeySelective(vo);
    }

    /**
     * 根据ID查询代理商通道详情
     * @param tradeChanId
     * @return
     */
    @Override
    public SysAgentChannel queryChannelById(Long tradeChanId) {
        SysAgentChannel sysAgentChannel = sysAgentChannelDao.selectByPrimaryKey(tradeChanId);
        return sysAgentChannel;
    }


}
